import {formatDateTime} from "@/utils/time.ts";
import {useMenuStore} from "@/store/menusStroe.ts";
import {deleteMenu} from "@/api/menu";
import {ElMessageBox, ElNotification} from "element-plus";


export default function () {

    enum MenuIcon {
        HomeFilled = IconEpHomeFilled,
        Lock = IconEpLock,
        Goods = IconEpGoods,
        User = IconEpUser,
        Avatar = IconEpAvatar,
        UserFilled = IconEpUserFilled,
        Tickets = IconEpTickets,
        ShoppingBag = IconEpShoppingBag,
        PriceTag = IconEpPriceTag,
        Sell = IconEpSell,
        SoldOut = IconEpSoldOut,
    }

    const menuStore = useMenuStore()

    const addMenuItem = () => {
        menuStore.addMenu_drawer = true
    }
    const addChildrenMenuItem = (item: any) => {
        menuStore.addChildMenu_drawer = true
        menuStore.parentMenu.ID = item.ID
        menuStore.parentMenu.name = item.name
        menuStore.parentMenu.level = item.level + 1
        menuStore.addChildType = item.type
    }
    const editMenuItem = (item: any) => {
        menuStore.editMenu_drawer = true
        menuStore.parentMenu.ID = item.ID
        menuStore.parentMenu.name = item.name
        menuStore.parentMenu.sort = item.sort
        menuStore.parentMenu.icon = item.icon
        menuStore.parentMenu.level = item.level
        menuStore.parentMenu.permissions = item.permissions
        menuStore.parentMenu.url = item.url
        menuStore.parentMenu.parent_id = item.parent_id
        menuStore.parentMenu.parent_name = item.parent_name
        menuStore.parentMenu.route_name = item.route_name
        menuStore.parentMenu.component = item.component
        menuStore.parentMenu.request_type = item.request_type
        menuStore.editType = item.type
    }
    const deleteMenuItem = (item: any) => {

        ElMessageBox.confirm(
            '确定删除' + item.name + '？',
            {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
            .then(() => {
                deleteMenu(item.ID).then(async (res) => {
                    if (res.code == 200) {
                        ElNotification({
                            message: res.msg,
                            type: 'success',
                        })

                        await menuStore.getMenuListStore()
                    } else {
                        ElNotification({
                            message: res.msg,
                            type: 'error',
                        })

                    }
                }).catch(() => {

                })

            })
    }
    // 页面加载时获取菜单项列表
    onBeforeMount(async () => {
        // 获取菜单项列表
        // 如果菜单项列表为空，则获取菜单项列表，防止重复获取
        if (menuStore.menuItems.length == 0) {
            await menuStore.getMenuListStore()
        }
    })


    function getString(icon: string): MenuIcon {
        return MenuIcon[icon as keyof typeof MenuIcon];
    }


    const typeToString = (row: any): string => {
        switch (row.type) {
            case 1:
                return '菜单';
            case 2:
                return '按钮';
            case 3:
                return 'API';
            default:
                // 可以根据需要处理其他值的情况，比如返回一个默认值或者抛出错误
                return '未知类型';
        }
    };


    const formatCATTime = (row: any): string => {
        return formatDateTime(row.CreatedAt)
    };

    const formatUPDTime = (row: any): string => {
        if (row.UpdatedAt == '0001-01-01T00:00:00Z') {
            return ''
        }
        return formatDateTime(row.UpdatedAt)
    }

    return {
        menuStore,
        addMenuItem,
        editMenuItem,
        addChildrenMenuItem,
        deleteMenuItem,
        getString,
        typeToString,
        formatCATTime,
        formatUPDTime,

    }
}